﻿using System;
using System.Collections.Generic;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Drawing;
using System.Linq;
using gpuHough.DirectX;

namespace gpuHough
{
    class TexturedRect : Rect<CustomVertex.PositionTextured>
    {
        private Matrix rotationOnTexture;

        private Vector2 sizeOnTexture;
        private Vector2 positionOnTexture;

        public TexturedRect(Vector2 size, Vector2 position, Vector2 sizeOnTexture, Vector2 positionOnTexture, float rotationOnTextureAngle)
            : base(size, position)
        {
            this.sizeOnTexture = sizeOnTexture;
            this.positionOnTexture = positionOnTexture;

            Matrix textureRotation = Matrix.RotationZ(rotationOnTextureAngle);
            Matrix textureTranslation = Matrix.Translation(-0.5f, -0.5f, 0);
            rotationOnTexture = textureTranslation * textureRotation * Matrix.Invert(textureTranslation);
        }

        protected override CustomVertex.PositionTextured BuildRectVertex(Vector3 cornerPosition, int cornerIndex)
        {   
            Vector2 cornerTextureCoordinate = positionOnTexture + new Vector2(sizeOnTexture.X * CornerPositions[cornerIndex].X, sizeOnTexture.Y * CornerPositions[cornerIndex].Y);

            cornerTextureCoordinate = Vector2.TransformCoordinate(cornerTextureCoordinate, rotationOnTexture);

            return new CustomVertex.PositionTextured(cornerPosition, cornerTextureCoordinate.X, cornerTextureCoordinate.Y);
        }
               
        public override VertexFormats GetVertexFormat()
        {
            return CustomVertex.PositionTextured.Format;
        }
    }
}